<template>
  <div class="param-main">
    <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="100px">
      <el-row :span="24">
        <el-col :span="6">
          <el-form-item label="技能名称">
            <el-input v-model="tblMain.param.skillTypeName" size="small" placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="handleAdd()">新增</el-button>
        <el-button size="small" type="danger" @click="deleteEvent()">删除</el-button>
      </div>
    </el-form>
    <div style="height: calc(100% - 160px);">
      <el-table ref="tblMain" style="margin-top: 10px" :data="tblMain.items" @selection-change="handleSelectionChange"
        v-loading.body="tblMain.loading" border stripe height="100%">
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="220px">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="deleteRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="skillTypeName" width="280" label="技能名称" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="parentName" width="280" label="技能分类" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="skillPriceType" width="280" label="单价类型" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.skillPriceType == '0'">时薪</span>
            <span v-else-if="scope.row.skillPriceType == '1'">件薪</span>
          </template>
        </el-table-column>
        <el-table-column prop="skillDesc" label="技能说明" align="center" show-overflow-tooltip></el-table-column>
      </el-table>
      <el-pagination style="margin-top: 10px" :current-page.sync="tblMain.currentPage" :page-size="tblMain.pageSize"
        :total="tblMain.total" layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
    </div>
    <skill-dialog ref="modalForm" @ok="modalFormOk" :exterOrg="exterOrg"></skill-dialog>
  </div>
</template>
<script>
import SkillDialog from './SkillDialog'
import tool from '@/tool'
import { storeSkillTypeList, storeSkillTypeDel } from '@/api/index'
import {
  getOutSideOrg
} from '@/api/businessCo'
export default {
  name: 'SkillList',
  components: {
    SkillDialog
  },
  data () {
    return {
      tblMain: tool.genTableData({
        param: {
          skillTypeName: ''
        },
        items: []
      }),
      exterOrg: {}
    }
  },
  mounted () {
    this.getOutSideOrg().then(() => {
      this.initTableData()
    })
  },
  methods: {
    handleAdd () {
      this.$refs.modalForm.add()
    },
    handleEdit (val) {
      this.$refs.modalForm.edit(val)
    },
    handleSetFunction (val) {
      this.$refs.roleFunction.set(val)
    },
    modalFormOk () {
      this.initTableData()
    },
    async deleteEvent () {
      if (this.tblMain.selection.length == 0) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('确定删除选中数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = []
          this.tblMain.selection.forEach((element, index) => {
            params.push(element.id)
          })
          await this.deleteTblMain(params)
        })
        .catch(() => { })
    },
    // 单个删除
    async deleteRow (val) {
      this.$confirm('确定删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = []
          params.push(val.id)
          await this.deleteTblMain(params)
        })
        .catch(() => { })
    },
    // 删除数据
    deleteTblMain (params) {
      let me = this
      me.tblMain.loading = true
      storeSkillTypeDel(params)
        .then((res) => {
          me.tblMain.loading = false
          let data = res.data
          if (data && data.code == 0) {
            me.$message.success('删除成功')
            this.initTableData()
          }
        })
        .catch(() => {
          me.$message.error('请求失败')
          me.tblMain.loading = false
        })
    },
    dataReset () {
      this.tblMain.currentPage = 1
      this.tblMain.pageSize = 10
      this.tblMain.param.skillTypeName = ''
      this.initTableData()
    },
    handleSelectionChange (val) {
      this.tblMain.selection = val
    },
    handleSizeChange (val) {
      this.tblMain.pageSize = val
      this.initTableData()
    },
    handleCurrentChange (val) {
      this.tblMain.currentPage = val
      this.initTableData()
    },
    initTableData () {
      let me = this
      me.tblMain.selection = []
      let param = {
        skillTypeName: me.tblMain.param.skillTypeName,
        storeId: me.exterOrg.id,
        page: me.tblMain.currentPage,
        size: me.tblMain.pageSize
      }
      me.tblMain.loading = true
      storeSkillTypeList(param).then((res) => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.tblMain.items = data.data.record
          me.tblMain.total = data.data.total
        }
      })
    },
    getOutSideOrg () {
      return new Promise(resolve => {
        getOutSideOrg().then(res => {
          const { code = undefined, data = [] } = res.data
          if (!code) {
            this.exterOrg = data[0];
            resolve()
          } else {
            this.$message.error(data)
          }
        })
      })

    }
  }
}
</script>
